package com.wuxin.ssm.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wuxin.ssm.entity.*;
import com.wuxin.ssm.mapper.UsersMapper;
import com.wuxin.ssm.service.UsersService;
import com.wuxin.ssm.vo.UsersVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: untitled
 * @ClassName UsersServiceImpl
 * @description:
 * @author: 李尧坤
 * @create: 2024−12-10 19:32
 * @Version 1.0
 **/
@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Override
    public int findByUname(String uname) {
        int num = usersMapper.selectByUnameNum(uname);
        return num;
    }

    @Override
    public void addUser(Users user) {
        usersMapper.insert(user);
    }

    @Override
    public Map<String, Object> login(String uname, String upass) {

        Map<String,Object> map = new HashMap<>();

        //使用对象
        UsersExample example =  new UsersExample();
        //使用条件对象
        example.createCriteria().andUnameEqualTo(uname);
        //得到集合
        List<Users> usersList = usersMapper.selectByExample(example);

        if (usersList != null && usersList.size()>0) {
            //集合中必然有数据
            Users users = usersList.get(0);
            //对比密码
            if (upass.equals(users.getUpass())) {
                //密码相等
                map.put("user",users);
                map.put("s","ok");
            }else {
                map.put("s","notok");//success error
            }

        }

        return map;
    }

    @Override
    public PageInfo splitPage(Integer page, int pageSize) {
        //对象是空的
        UsersExample example = new UsersExample();
        //使用分页 必须先设置这个方法
        PageHelper.startPage(page,pageSize);
        //查询所有数据
        List<Users> orderdetailList = usersMapper.selectAll(example);
        //将集合交给分页类
        PageInfo<Users> pageInfo= new PageInfo<Users>(orderdetailList);

        return pageInfo;
    }

    @Override
    public PageInfo chaxun(Integer page, int pageSize, UsersVo usersVo) {
        //使用分页 必须先设置这个方法
        PageHelper.startPage(page,pageSize);

        List<Users> usersList = usersMapper.selectCondition(usersVo);

        //将集合交给分页类
        PageInfo<Users> pageInfo= new PageInfo<Users>(usersList);

        return pageInfo;
    }
}
